package com.xinqi.modules.sys.config.controller.admin;

import com.xinqi.common.base.constants.ServiceNameConstants;
import com.xinqi.common.base.response.PageInfo;
import com.xinqi.common.base.response.R;
import com.xinqi.common.core.web.controller.BaseController;
import com.xinqi.common.log.syslog.annotation.Log;
import com.xinqi.common.log.syslog.enums.BusinessType;
import com.xinqi.common.log.syslog.enums.OperatorType;
import com.xinqi.modules.sys.config.domain.SysConfig;
import com.xinqi.modules.sys.config.dto.req.SysConfigQuery;
import com.xinqi.modules.sys.config.service.ISysConfigService;
import lombok.RequiredArgsConstructor;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * ADMIN -- 参数配置
 *
 * @author dgyu
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/admin/config")
public class SysConfigAdminController extends BaseController {

    private final ISysConfigService configService;

    @GetMapping("/selectPage")
    public R<PageInfo<SysConfig>> selectPage(@ParameterObject SysConfigQuery query) {
        PageInfo<SysConfig> page = configService.page(query, query);
        return R.data(page);
    }

    /**
     * 根据ID获取参数配置
     *
     * @param configId 配置ID
     */
    @GetMapping(value = "/configId/{configId}")
    public R<SysConfig> getConfigKey(@PathVariable("configId") Long configId) {
        return R.ok(configService.selectConfigById(configId));
    }

    /**
     * 根据参数键名查询参数值
     *
     * @param configKey 配置key
     */
    @GetMapping(value = "/configKey/{configKey}")
    public R<String> getConfigKey(@PathVariable("configKey") String configKey) {
        return R.data(configService.selectConfigByKey(configKey), "获取成功");
    }

    /**
     * 参数修改
     *
     * @param sys
     * @return
     */
    @Log(title = ServiceNameConstants.XQ_SYS + " -- 修改参数配置", businessType = BusinessType.UPDATE, operatorType = OperatorType.MANAGE)
    @PostMapping(value = "/edit")
    public R<String> editConfigKey(@RequestBody SysConfig sys) {
        return R.ok(configService.updateConfig(sys));
    }

    /**
     * 刷新参数缓存
     */
    @Log(title = ServiceNameConstants.XQ_SYS + " -- 重置参数配置Redis缓存", businessType = BusinessType.UPDATE, operatorType = OperatorType.MANAGE)
    @DeleteMapping("/refreshCache")
    public R<Void> refreshCache() {
        configService.resetConfigCache();
        return R.ok();
    }

}
